package com.fh.fruit.service.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.service.user.FruitUserManager;
import com.fh.fruit.util.CustomUtil;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.MD5;
import com.fh.util.PageData;


@Service("fruitUserService")
public class UserService implements FruitUserManager {

	@Resource(name = "daoSupport")
	private DaoSupport dao;

	// 用户注册
	@Override
	public void saveUser(PageData pd) throws Exception {
		String phone = pd.getString("phone");
		String pwd = pd.getString("pwd");
		pd.put("pwd", MD5.md5(pwd));
		pd.put("userName", phone);
		dao.save("FruitUserMapper.insertUserLogin", pd);
		System.out.println("登录id===============" + pd.get("id"));
		String loginId = pd.get("id").toString();
		String petName = CustomUtil.subPhone(phone);
		pd.put("petName", petName);
		pd.put("loginId", loginId);
		pd.put("phone", phone);
		pd.put("companyName", "");
		pd.put("address", "");
		pd.put("creationDate", "");
		pd.put("isAuthentication", "01");
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("codePre", DateUtil.getYear());// 前缀
		pd.put("codeSuf", CustomUtil.buildRandom(3) + "");// 后缀
		dao.save("FruitUserMapper.insertUserInfo", pd);
		System.out.println("用户id======================" + pd.get("id"));
		String userId = pd.get("id").toString();
		pd.put("userId", userId);
		pd.put("loginId", loginId);
		dao.save("FruitUserMapper.insertUserAccount", pd);
	}

	/**
	 * 保存用户
	 * 
	 * @param pd
	 * @
	 */
	public void saveU(PageData pd) throws Exception {
		dao.save("FruitUserMapper.insertUserInfo", pd);
	}

	/**
	 * 添加登录账号
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void savaLogin(PageData pd) throws Exception {
		dao.save("FruitUserMapper.insertUserLogin", pd);
	}

	/**
	 * 添加账户信息
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void savaAccount(PageData pd) throws Exception {
		dao.save("FruitUserMapper.insertUserAccount", pd);
	}

	/**
	 * 修改用户
	 * 
	 * @param pd
	 * @
	 */
	public void editU(PageData pd) throws Exception {
		dao.update("FruitUserMapper.editU", pd);
	}

	/**
	 * 删除用户
	 * 
	 * @param pd
	 * @
	 */
	public void deleteU(PageData pd) throws Exception {
		dao.delete("FruitUserMapper.deleteU", pd);
	}

	/**
	 * 批量删除用户
	 * 
	 * @param USER_IDS
	 * @
	 */
	public void deleteAllU(String[] USER_IDS) throws Exception {
		dao.delete("FruitUserMapper.deleteAllU", USER_IDS);
	}

	/**
	 * 用户列表(全部)
	 * 
	 * @param USER_IDS
	 * @
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listAllUser(PageData pd) throws Exception {
		return (List<PageData>) dao.findForList("FruitUserMapper.listAllUser", pd);
	}

	/**
	 * 会员列表
	 * 
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String ,Object>> listfruitUsers(Page page) throws Exception {
		List<Map<String ,Object>> userList = new ArrayList<Map<String ,Object>>();
		PageData pd =page.getPd();
		String endTime=pd.getString("endTime");
			if(endTime != null && !"".equals(endTime)){
				pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
				page.setPd(pd);
			}
		userList=(List<Map<String ,Object>>) dao.findForList("FruitUserMapper.fruitUserlistPage", page);
		for (Map<String, Object> map : userList) {
			String birthday = (String)map.get("birthday");
			if(birthday != null){
				map.put("ageNum", DateUtil.getAgeByBirthDay(birthday));
			} else{
				map.put("ageNum", "0");
			}
		}
		return userList;
	}

	@Override
	public void updateUserState(PageData pd) throws Exception {
		dao.update("FruitUserMapper.updateUserState", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>> listJoins(Page page) throws Exception {
		return  (List<Map<String,Object>>) dao.findForList("FruitUserMapper.joinlistPage", page);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getJoinsMsg(PageData pd) throws Exception {
		dao.update("FruitUserMapper.updateJoinState", pd);
		return (Map<String, Object>) dao.findForObject("FruitUserMapper.getJoinsMsg", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getProblemList(Page page) throws Exception {
		return (List<Map<String, Object>>) dao.findForList("FruitUserMapper.problemlistPage", page);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getProblem(PageData pd) throws Exception {
		return (Map<String,Object>) dao.findForObject("FruitUserMapper.getProblem", pd);
	}

	@Override
	public int updateEdit(PageData pd) throws Exception {
		return (int) dao.update("FruitUserMapper.updateEdit", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getProblemParent(PageData pd) throws Exception {
		return (Map<String, Object>) dao.findForObject("FruitUserMapper.getProblemParent", pd);
	}

	@Override
	public void saveProblem(PageData pd) throws Exception {
		dao.save("FruitUserMapper.saveProblem", pd);
	}

	@Override
	public Object getProblemParentId(PageData pd) throws Exception {
		return (String) dao.findForObject("FruitUserMapper.getProblemParentId", pd);
	}

	@Override
	public void delProblem(PageData pd) throws Exception {
		dao.delete("FruitUserMapper.delProblem", pd);
	}

	@Override
	public int getRegisterNum(PageData pd) throws Exception {
		return (int) dao.findForObject("FruitUserMapper.getRegisterNum",pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> listConsumeUsers(Page page) throws Exception {
		PageData pd =page.getPd();
		String endTime=pd.getString("endTime");
			if(endTime != null && !"".equals(endTime)){
				pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
				page.setPd(pd);
			}
		return (List<Map<String, Object>>) dao.findForList("FruitUserMapper.consumeUserlistPage", page);
	}

	@Override
	public int getConsumeNum(PageData pd) throws Exception {
		return (int) dao.findForObject("FruitUserMapper.getConsumeNum",pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getUserDetails(PageData pd) throws Exception {
		return (Map<String, Object>) dao.findForObject("FruitUserMapper.getUserDetails", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getWholeDetails(PageData pd) throws Exception {
		Map<String ,Object> map =new HashMap<String, Object>();
		pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
		map=(Map<String, Object>) dao.findForObject("FruitUserMapper.getWholeDetails", pd);
		Map<String ,Object> maps=(Map<String, Object>) dao.findForObject("FruitUserMapper.getWholeDetails1", pd);
		map.put("consumeNum", maps.get("consumeNum"));
		map.put("real_price", maps.get("real_price"));
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getBuyInfo(PageData pd) throws Exception {
		return (List<Map<String, Object>>) dao.findForList("FruitUserMapper.getBuyInfo", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getInviteInfo(PageData pd) throws Exception {
		return (Map<String,Object>) dao.findForObject("FruitUserMapper.getInviteInfo", pd);
	}

	/*@SuppressWarnings("unchecked")
	@Override
	public Map<String,Object> getUserAge(PageData pd) throws Exception {
		 Map<String,Object> res= new HashMap<>();
		 List<Map<String,Object>> maps=(List<Map<String,Object>>)dao.findForList("FruitUserMapper.getUserAge", pd);
		String year1=DateUtil.getAfterYears("-30");
		String year2=DateUtil.getAfterYears("-50");
		int n1=0;int n2=0;int n3=0;
		NumberFormat numberFormat = NumberFormat.getInstance();  
	    numberFormat.setMaximumFractionDigits(2);  
		for (Map<String, Object> map : maps) {
			String birthday=(String)map.get("birthday");
			if(year1.compareTo(birthday)>0){
				n1++;
			} else if(year2.compareTo(birthday)>0 && year1.compareTo(birthday)<0){
				n2++;
			} else if(year2.compareTo(birthday)<0){
				n3++;
			}
		}
        String n11 = numberFormat.format((float) n1 / (float) maps.size() * 100); 
        String n22 = numberFormat.format((float) n2 / (float) maps.size() * 100); 
        String n33 = numberFormat.format((float) n3 / (float) maps.size() * 100);
        res.put("", value)
		return res;
	}*/
	
	@SuppressWarnings("unchecked")
	@Override
	public Map<String,Object> getUserAge(PageData pd) throws Exception {
		return (Map<String,Object>)dao.findForObject("FruitUserMapper.getUserAge", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getUserRegion(PageData pd) throws Exception {
		return (List<Map<String, Object>>) dao.findForList("FruitUserMapper.getUserRegion", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getRatio(PageData pd) throws Exception {
		return (Map<String, Object>) dao.findForObject("FruitUserMapper.getRatio", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getInviteNum(PageData pd) throws Exception {
		List<Map<String, Object>> res=null;
		String state=(String)pd.get("state");
		if("1".equals(state)){
			res=(List<Map<String,Object>>) dao.findForList("FruitUserMapper.getInviteNum", pd);
		} else if("2".equals(state)){
			res=(List<Map<String,Object>>) dao.findForList("FruitUserMapper.getInviteNum1", pd);
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> listUsers(Page page) throws Exception {
		return (List<Map<String, Object>>) dao.findForList("FruitUserMapper.listUserslistPage", page);
	}
}
